import { exercises } from "@/js/exercises";
import axios from "axios";
import {
    ElMessage,
} from "element-plus";

const baseURL = '/api';  // 前端可访问的完整后端路径
const instance = axios.create({
    baseURL,
    timeout: 30000,
});


export async function getAllExercise() {

    try {
        const result = await instance.get("/exercises/getAll");


        return result.data;


    } catch (err) {
        console.error("Error fetching users:", err);
    }
}

//删除用户
export async function exerciseDeleteService(exerciseId) {
    try {
        const result = await instance.delete(`/exercises/delete/${exerciseId}`);

        return result.data;
    } catch (err) {
        console.error("Error deleting user:", err);
        throw error;
    }
}

export async function exerciseSearchService(exerciseName) {
    try {
        const result = await instance.get(`/exercises/Search?exercisename=${exerciseName}`);

        return result.data;
    } catch (err) {
        console.error("Error fetching users:", err);

    }
}

export async function exercisesAddService(exercises) {
    try {
        const result = await instance.post("/exercises/add", exercises);
        console.log(result.data);
        return result.data;
    } catch (err) {
        // 打印错误响应信息
        console.error("Error response:", err.response?.data);
    }
}

export async function exerciseUpdateService(id, exercises) {
    try {
        const response = await instance.put(`/exercises/update/${id}`, exercises, {
            headers: {
                'Content-Type': 'application/json',
            },
        });

        if (response.data) {
            ElMessage.success('反馈成功');
        } else {
            ElMessage.error('更新失败：未找到相关记录');
        }
    } catch (error) {
        console.error('更新失败', error);
        ElMessage.error('更新失败');
    }
}

